package com.oocl.RTManageServer.manager.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Element;
import org.json.JSONException;
import org.json.JSONObject;

import com.oocl.RTManageServer.manager.ChatServerManager;
import com.oocl.RTManageServer.responser.HttpResponser;
import com.oocl.RTManageServer.util.HtmlGenerator;
import com.oocl.constant.ProtocolType;
import com.oocl.pojo.Protocol;
import com.oocl.util.JsonUtil;
import com.oocl.util.ResourceCloser;

public class ChatServerManagerImpl implements ChatServerManager{
	private HttpResponser responser;
	
	public ChatServerManagerImpl(HttpResponser responser) {
		this.responser = responser;
	}

	@Override
	public void processStart() throws IOException {
		Protocol pro1 = new Protocol();
		pro1.setType(ProtocolType.START_CHAT);
		pro1.setTime(new Date());
		Protocol pro2 = sendRequest(pro1);
		List<Element> eles = HtmlGenerator.getCommonHtml();
		Element body = eles.get(1);
		Element p = new Element("p");
		p.setText(pro2.getContent());
		body.addContent(p);
		responser.response(eles.get(0));
	}

	@Override
	public void processStop() throws IOException {
		Protocol pro1 = new Protocol();
		pro1.setType(ProtocolType.STOP_CHAT);
		pro1.setTime(new Date());
		Protocol pro2 = sendRequest(pro1);
		
		List<Element> eles = HtmlGenerator.getCommonHtml();
		Element body = eles.get(1);
		Element p = new Element("p");
		p.setText(pro2.getContent());
		body.addContent(p);
		responser.response(eles.get(0));
	}

	@Override
	public void getServerTime() throws IOException {
		Protocol pro1 = new Protocol();
		pro1.setType(ProtocolType.GET_CHAT_TIME);
		pro1.setTime(new Date());
		Protocol pro2 = sendRequest(pro1);
		
		List<Element> eles = HtmlGenerator.getCommonHtml();
		Element body = eles.get(1);
		Element p = new Element("p");
		if(pro2.getType()==ProtocolType.GET_CHAT_TIME){
			p.setText("serverTime:"+pro2.getContent()+" milliseonds");
		}else {
			p.setText(pro2.getContent());
		}
		body.addContent(p);
		responser.response(eles.get(0));
	}

	@Override
	public void getUserList() throws IOException {
		Protocol pro1 = new Protocol();
		pro1.setType(ProtocolType.LOAD_USERS);
		pro1.setTime(new Date());
		Protocol pro2 = sendRequest(pro1);
		
		List<Element> eles = HtmlGenerator.getCommonHtml();
		Element body = eles.get(1);
		Element div = new Element("div");
		if(pro2.getType()==ProtocolType.LOAD_USERS){
			ArrayList<String> users = pro2.getOnlineUsers();
			Element table = new Element("table");
			Element tr1 = new Element("tr");
			Element td1 = new Element("td");
			td1.setText("Online Users");
			tr1.addContent(td1);
			table.addContent(tr1);
			for (String user : users) {
				Element tr = new Element("tr");
				Element td = new Element("td");
				td.setText(user);
				tr.addContent(td);
				table.addContent(tr);
			}
			div.addContent(table);
		}else {
			div.setText(pro2.getContent());
		}
		body.addContent(div);
		responser.response(eles.get(0));
	}
	
	private Protocol sendRequest(Protocol pro){
		Protocol protocol = null;
		Socket socket = null;
		BufferedReader reader = null;
		PrintWriter writer = null;
		try {
			socket = new Socket("127.0.0.1", 6666);
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer = new PrintWriter(socket.getOutputStream());
			writer.println(JsonUtil.toJsonObject(pro).toString());
			writer.flush();
			
			String line = reader.readLine();
			protocol = JsonUtil.toProtocol(new JSONObject(line.trim()));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ResourceCloser.release(writer, reader, socket, null);
		}
		return protocol;
	}

	@Override
	public void defaultView() throws IOException {
		List<Element> eles = HtmlGenerator.getCommonHtml();
		responser.response(eles.get(0));
	}
	
}
